<?php
/**
 * Create By: PhpStorm
 * User: yuanxuncheng
 * File: GetComment.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\comment;

use app\api\model\dao\Comment;
use app\api\model\dao\UserAccount;
use app\library\Error;
use app\library\PageBase;
use think\Validate;
use think\Cache;

class GetCommentByGoodsid extends PageBase
{

    private $objComment;
    private $objUserAccount;

    public function __construct()
    {
        parent::__construct();

        $this->objComment = new Comment();
        $this->objUserAccount = new UserAccount();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'goods_id' => 'require',
        ];
        $msg = [
            'goods_id.require' => '商品ID必填',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {
        // TODO:业务逻辑
        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }

        $commend_rank = '1,2,3,4,5';
        if (!empty($param['comment_tag'])) {
            if ($param['comment_tag'] == 'good') {
                $commend_rank = '5';
            } else if ($param['comment_tag'] == 'normal') {
                $commend_rank = '3,4';
            } else if ($param['comment_tag'] == 'bad') {
                $commend_rank = '1,2';
            }
        }

        $goods_id = isset($param['goods_id']) ? intval($param['goods_id']) : 0;

        $arrField = array();
        $arrCond = array();
        $arrCond['comment_type'] = ['=', 0];
        $arrCond['id_value'] = ['=', $goods_id];
        $arrCond['comment_rank'] = ['in', $commend_rank];

        $perpage = isset($param['perpage']) ? intval($param['perpage']) : 0;
        $page = isset($param['page']) ? intval($param['page']) : 0;
        $perpage = max(10, $perpage);
        $page = max(1, $page);
        $offset = ($page - 1) * $perpage;
        $limit = $perpage;

        $counts = $this->objComment->getCount($arrCond);
        $ret = $this->objComment->getCommentByCond($arrCond, $arrField, $offset, $limit, ' add_time desc ');

        $count = $this->objComment->getCount(['id_value' => ['=', $goods_id]]);
        $sum = $this->objComment->getSum(['id_value' => ['=', $goods_id]]);
        $badcount = $this->objComment->getCount(['id_value' => ['=', $goods_id], 'comment_rank' => '1,2']);
        $normalcount = $this->objComment->getCount(['id_value' => ['=', $goods_id], 'comment_rank' => '3,4']);
        $goodcount = $this->objComment->getCount(['id_value' => ['=', $goods_id], 'comment_rank' => '5']);

        if (false === $ret) {
            return $this->getRes(Error::ERR_SYS);
        }

        $avg = $count ? round($sum / $count, 2) : 0;

        $this->data = array(
            'list' => $ret,
            'total' => $counts,
            'avg' => $avg,
            'good' => $goodcount,
            'normal' => $normalcount,
            'bad' => $badcount,
            'all' => $count,
        );

        return $this->getRes();
    }
}
